package logic

import (
	"context"
	"encoding/json"
	"github.com/olivere/elastic/v7"
	"zg_four_week3/esserver/esserverclient"

	"zg_four_week3/esclient/internal/svc"
	"zg_four_week3/esclient/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type EsCheckLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewEsCheckLogic(ctx context.Context, svcCtx *svc.ServiceContext) *EsCheckLogic {
	return &EsCheckLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *EsCheckLogic) EsCheck(req *types.EsCheckRequest) (resp *types.EsCheckResponse, err error) {
	// todo: add your logic here and delete this line
	index := req.Index
	word := req.Word
	var r types.EsCheckResponse
	r.Code = -1
	r.Data = nil
	res, err := l.svcCtx.EsServer.EsCheck(l.ctx, &esserverclient.EsCheckRequest{
		Index: index,
		Word:  word,
	})
	if err != nil {
		return &r, nil
	}
	var m *elastic.SearchResult
	json.Unmarshal(res.Data, &m)
	r.Code = 200
	r.Msg = "success"
	r.Data = m
	return &r, nil
}
